package com.spx.core.wizards;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.spx.core.types.User;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (mpe).
 */

public class NewBasicUserPage extends ModelWizardPage<User>
{
	User user = new User();
	private Text usernameText;

	private Text password1Text;
	private Text password2Text;

	private Button userEnabled;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public NewBasicUserPage()
	{
		super("wizardPage");
		setTitle("New User Basic Details");
		setDescription("This wizard creates a user.");

	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent)
	{
		final Composite container = new Composite(parent, SWT.NULL);
		final GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;
		Label label = new Label(container, SWT.NULL);
		label.setText("&User:");

		usernameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		usernameText.setLayoutData(gd);
		usernameText.addModifyListener(new ModifyListener()
		{
			public void modifyText(ModifyEvent e)
			{
				dialogChanged();
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("&Password:");

		password1Text = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		password1Text.setLayoutData(gd);
		password1Text.addModifyListener(new ModifyListener()
		{
			public void modifyText(ModifyEvent e)
			{
				dialogChanged();
			}
		});
		label = new Label(container, SWT.NULL);
		label.setText("&Repeat Password:");

		password2Text = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		password2Text.setLayoutData(gd);
		password2Text.addModifyListener(new ModifyListener()
		{
			public void modifyText(ModifyEvent e)
			{
				dialogChanged();
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("&Enabled:");

		userEnabled = new Button(container, SWT.CHECK);

		gd = new GridData(GridData.FILL_HORIZONTAL);

		userEnabled.setLayoutData(gd);
		userEnabled.addSelectionListener(new SelectionListener()
		{

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0)
			{
				dialogChanged();

			}

			@Override
			public void widgetSelected(SelectionEvent arg0)
			{
				dialogChanged();

			}
		});

		initialize();
		dialogChanged();
		setControl(container);
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */

	private void initialize()
	{

		password1Text.setText("dummy");

		password2Text.setText("dummy");
	}

	/**
	 * Ensures that both text fields are set.
	 */

	private void dialogChanged()
	{

		if (StringUtils.isBlank(this.usernameText.getText()))
		{
			updateStatus("User Name must be specified");

		} else if (StringUtils.equals(password1Text.getText(), password2Text.getText()) == false)
		{
			updateStatus("Passwords are not the same");
		} else
		{
			updateStatus(null);
		}

	}

	private void updateStatus(String message)
	{
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	public String getContainerName()
	{
		return usernameText.getText();
	}

	public String getFileName()
	{
		return password1Text.getText();
	}

	@Override
	public void setProperties(User user)
	{
		user.setName(this.usernameText.getText());
		user.setPassword(this.password1Text.getText());
		user.setDisabled(this.userEnabled.getSelection() == false);
	}

	@Override
	protected void createPageControl(Composite parent)
	{
		// TODO Auto-generated method stub

	}

}